/**
 * Copyright (c) 2012 Nokia Corporation.
 */

package com.nokia.example.productcatalog;

import java.util.Vector;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 * A utility class for reading from and write to a record store.
 */
public class BasicRMS {
	// Members
    private RecordStore rms = null;

    public void open(String rmsName) {
        try {
            rms = RecordStore.openRecordStore(rmsName, true);
        }
        catch (RecordStoreException rse) {
            rse.printStackTrace();
        }
    }

    public Vector get() {
        Vector result = new Vector();
        byte[] data = null;
        
        try {
            RecordEnumeration renum = rms.enumerateRecords(null, null, false);
            
            while (renum.hasNextElement()) {
                int index = renum.nextRecordId();
                data = rms.getRecord(index);
                String element = new String (data);
                result.addElement(element);
            }
        }
        catch (RecordStoreException rse) {
            rse.printStackTrace();
        }
        
        return result;
    }

    public void set(byte[] data) {
        try {
            rms.addRecord(data, 0, data.length);
        }
        catch (RecordStoreException rse) {
            rse.printStackTrace();
        }
    }

    public void close() {
        try {
            rms.closeRecordStore();
        }
        catch (RecordStoreException rse) {}
    }
}
